﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TLib.Xna;

namespace COMP477_Scorchery
{
    class Particle3D : GameObject
    {
        public Vector3 Velocity;
        public Vector3 Direction;
        public float Acceleration;
        public float MAX_SPEED;

        public float LifeSpan;
        public float Age;

        public float RotationSpeed;

        public Color Color;
        public float Alpha;
        public Texture2D Texture;

        public List<VertexPositionNormalTexture> vertices;

        private Vector2 topLeft = new Vector2(0.0f, 0.0f);
        private Vector2 topRight = new Vector2(1.0f, 0.0f);
        private Vector2 bottomLeft = new Vector2(0.0f, 1.0f);
        private Vector2 bottomRight = new Vector2(1.0f, 1.0f);

        public Particle3D(Texture2D texture) : base(null)
        {
            Velocity = new Vector3(0, 0, 0);
            Direction = new Vector3(0, 0, 0);

            vertices = new List<VertexPositionNormalTexture>(4);
            vertices.Add(new VertexPositionNormalTexture(new Vector3(0, 1, -1), new Vector3(-1, 0, 0), topLeft));
            vertices.Add(new VertexPositionNormalTexture(new Vector3(0, -1, -1), new Vector3(-1, 0, 0), bottomLeft));
            vertices.Add(new VertexPositionNormalTexture(new Vector3(0, -1, 1), new Vector3(-1, 0, 0), bottomRight));
            vertices.Add(new VertexPositionNormalTexture(new Vector3(0, 1, 1), new Vector3(-1, 0, 0), topRight));

            Acceleration = 2;
            MAX_SPEED = 3f;

            LifeSpan = 1;

            RotationSpeed = 0;

            Color = Color.White;

            Texture = texture;

            Init();
        }

        public virtual void Init()
        {
            Age = 0;
            Alpha = 1;
        }

        public virtual void Update(GameTime gameTime, GameObject camera)
        {
            float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (Direction != Vector3.Zero)
                Direction.Normalize();
            Direction *= Acceleration;

            Velocity += Direction * delta;
            Pitch += RotationSpeed * delta;
            Yaw = camera.Yaw + MathHelper.ToRadians(180);
            Roll = camera.Pitch - MathHelper.ToRadians(180);

            if (Velocity.Length() > MAX_SPEED)
            {
                Velocity.Normalize();
                Velocity *= MAX_SPEED;
            }

            Position += Velocity * delta;

            Age += delta;
        }
    }
}
